import antenna from "./antenna"
import routers from "./router"
const antennada = antenna.state
const routersdatas = routers.state
const accessterminal = {
  state: {
    accessterminalsdata: [],
    antennadatas: antennada || [],
    rcunattask: [],
    rcurouter: []
  },
  mutations: {
    getaccessterminalsdata(state, data) {
      console.log(data);
      state.accessterminalsdata = data.rcus || []
      state.antennadatas = state.antennadatas.antennadata
      console.log(state.accessterminalsdata);
      console.log(state.antennadatas);
    },
    getrcunattask(state, data) {
      state.rcunattask = []
      console.log(data);
      state.rcunattask.push(data)
      console.log(state.rcunattask);
    },
    getrcurouter(state, data) {
      state.rcurouter = []
      console.log(data);
      state.rcurouter.push(data)
      console.log(state.rcurouter);
    }
  },
  actions: {
    async getaccessterminalsdata(context, payload) {
      let response = await context.dispatch('axios', {
        method: 'get',
        url: '/api/sam/web/rcu'
      })
      context.commit('getaccessterminalsdata', response.data)
    },
    async getrcunattask(context, payload) {
      console.log(payload);
      let response = await context.dispatch('axios', {
        method: 'get',
        url: '/api/sam/web/rcu/nat_task',
        params: {
          rcu_id: payload
        }
      })
      context.commit('getrcunattask', response.data)
    },
    async addrcunattask(context, payload) {

      let response = await context.dispatch('axios', {
        method: 'post',
        url: '/api/sam/web/rcu/nat_task',
        data: {
          rcu_nat_task_ids: payload.rcu_nat_task_ids,
          rcu_id: payload.rcu_id
        }
      })
      context.commit('getrcunattask', response.data)
    },
    async deletercunattask(context, payload) {

      let response = await context.dispatch('axios', {
        method: 'delete',
        url: '/api/sam/web/rcu/nat_task',
        data: {
          rcu_nat_task_ids: payload.rcu_nat_task_ids,
          rcu_id: payload.rcu_id
        }
      })
     
    },


    async getrcurouter(context, payload) {
      console.log(payload);
      let response = await context.dispatch('axios', {
        method: 'get',
        url: '/api/sam/web/rcu/router_task',
        params: {
          rcu_id: payload
        }
      })
      context.commit('getrcurouter', response.data)
    },
    async deletercurouter(context, payload) {
      console.log(payload);
      let response = await context.dispatch('axios', {
        method: 'delete',
        url: '/api/sam/web/rcu/router_task',
        data: {
          antenna_id: payload.antenna_id,
          ip: payload.ip
        }
      })
    },
    async creatercurouter(context, payload) {
      console.log(payload);
      let response = await context.dispatch('axios', {
        method: 'post',
        url: '/api/sam/web/rcu/router_task',
        data: {
          rcu_nat_task_ids: payload.rcu_nat_task_ids,
          rcu_id: payload.rcu_id
        }
      })
    },
    async addrcunat(context, payload) {
      let response = await context.dispatch('axios', {
        method: 'post',
        url: '/api/sam/web/rcu',
        data: {
          antenna_id: payload.antenna_id,
          ip: payload.ip
        }
      })
    },
    async Editrcunat(context, payload) {
      let response = await context.dispatch('axios', {
        method: 'put',
        url: '/api/sam/web/rcu',
        data: {
          antenna_id: payload.antenna_id,
          ip: payload.ip,
          id: payload.id
        }
      })
    },
    async Deletercunat(context, payload) {
      let response = await context.dispatch('axios', {
        method: 'delete',
        url: '/api/sam/web/rcu',
        data: {
          id: payload.id
        }
      })
    }
  },
  getters: {
    antennaDataTsableList(state) {
      console.log(state.accessterminalsdata);
      console.log(state.antennadatas);
      return state.accessterminalsdata.map(antennaItems => {
        console.log(antennaItems.id);
        for (const item of state.antennadatas) {
          console.log(item);
          if (antennaItems.antenna_id === item.id) {
            console.log(antennaItems);
            return {
              ...antennaItems,
              name: item.name,
            }
          }
        }
      })
    },
    rcunattaskdatas(state) {
      if (state.rcunattask.length == 0) {
        return [];
      } else {
        return state.rcunattask[0].nat_tasks
      }
    },
    rcurouterdatas(state) {
      console.log(state.rcurouter);
      if (state.rcurouter.length == 0) {
        return [];
      } else {
        return state.rcurouter[0].router_tasks
      }
    },
    rcuroutedialog(state){
      return  concat_arr(routersdatas.routersdata.router_tasks,state.rcurouter[0].router_tasks)
      function concat_arr(arr1, arr2) {
          var result = [];
          for (var i = 0; i < arr1.length; i++) {
            var obj = arr1[i];
            var num = obj.id;
            var flag = false;
            for (var j = 0; j < arr2.length; j++) {
              var aj = arr2[j];
              var n = aj.id;
              if (n == num) {
                flag = true;
                break;
              }
            }
            if (!flag) {
              result.push(obj);
            }
          }
          return result
        }
        
    }

  }
}
export default accessterminal